package com.example.thymeleaf.controller;

import com.example.thymeleaf.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class PartFreshController {
    final List<Object> list = Arrays.asList(
            new User(1, "a"),
            new User(2, "b"),
            new User(3, "c"),
            new User(4, "d"),
            new User(5, "e")
    );

    // 获取整个页面
    @GetMapping("userPage")
    public String userPage(Model model) {
        model.addAttribute("users", list.stream().limit(2).collect(Collectors.toList()));
        return "partFresh";
    }

    // 局部刷新部分table
    @GetMapping("getUser")
    public String getUser(Integer pageNum, Model model) {
        final List<Object> list = Arrays.asList(
                new User(1, "a"),
                new User(2, "b"),
                new User(3, "c"),
                new User(4, "d"),
                new User(5, "e")
        );
        model.addAttribute("users", list.stream().skip((pageNum - 1) * 2L).limit(2).collect(Collectors.toList()));
        return "partFresh::user-list";
    }
}
